/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BusquedaEstudiante.java
 *
 * Created on 30/08/2011, 02:53:43 PM
 */
package Estudiante;

import Reportes.Report;
import bufete.Ingresar_alumno;
import callProcs.ProcsCliente;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;









public class BusquedaEstudiante extends javax.swing.JFrame {

    public java.sql.ResultSet rs ;
    public String consulta = new String();
    public ProcsCliente verCliente = new ProcsCliente();
    public DefaultTableModel nombres = new DefaultTableModel();
    public DefaultTableColumnModel columnas = new DefaultTableColumnModel(); 
    public String Usuario;
    
    /** Creates new form BusquedaEstudiante */
    public BusquedaEstudiante() {
        initComponents();
    }
    
    public void SetUsuario(String user){
        Usuario = user;
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        tBusqueda = new javax.swing.JTextField();
        bBuscar = new javax.swing.JButton();
        nCarne = new javax.swing.JRadioButton();
        rNombre = new javax.swing.JRadioButton();
        nCedula = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tEstudiante = new javax.swing.JTable();
        bNuevo = new javax.swing.JButton();
        bEditar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(bufete.BufeteApp.class).getContext().getResourceMap(BusquedaEstudiante.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setName("jPanel1"); // NOI18N

        tBusqueda.setText(resourceMap.getString("tBusqueda.text")); // NOI18N
        tBusqueda.setName("tBusqueda"); // NOI18N

        bBuscar.setIcon(resourceMap.getIcon("bBuscar.icon")); // NOI18N
        bBuscar.setText(resourceMap.getString("bBuscar.text")); // NOI18N
        bBuscar.setName("bBuscar"); // NOI18N
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });

        nCarne.setText(resourceMap.getString("nCarne.text")); // NOI18N
        nCarne.setName("nCarne"); // NOI18N
        nCarne.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nCarneActionPerformed(evt);
            }
        });

        rNombre.setText(resourceMap.getString("rNombre.text")); // NOI18N
        rNombre.setName("rNombre"); // NOI18N
        rNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rNombreActionPerformed(evt);
            }
        });

        nCedula.setText(resourceMap.getString("nCedula.text")); // NOI18N
        nCedula.setName("nCedula"); // NOI18N
        nCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nCedulaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(114, 114, 114)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(nCarne)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addComponent(rNombre)
                        .addGap(29, 29, 29)
                        .addComponent(nCedula)
                        .addGap(85, 85, 85))
                    .addComponent(tBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, 366, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(50, 50, 50)
                .addComponent(bBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(127, 127, 127))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nCarne)
                    .addComponent(nCedula)
                    .addComponent(rNombre))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bBuscar))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tEstudiante.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "No.Carné", "No. Bufete", "Nombre", "No. Cédula", "Telefono", "Correo Electronico"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tEstudiante.setFocusable(false);
        tEstudiante.setName("tEstudiante"); // NOI18N
        tEstudiante.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tEstudiante);
        tEstudiante.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tEstudiante.columnModel.title0")); // NOI18N
        tEstudiante.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tEstudiante.columnModel.title5")); // NOI18N
        tEstudiante.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tEstudiante.columnModel.title1")); // NOI18N
        tEstudiante.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("tEstudiante.columnModel.title2")); // NOI18N
        tEstudiante.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("tEstudiante.columnModel.title3")); // NOI18N
        tEstudiante.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("tEstudiante.columnModel.title4")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 708, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(46, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(102, Short.MAX_VALUE))
        );

        bNuevo.setIcon(resourceMap.getIcon("bNuevo.icon")); // NOI18N
        bNuevo.setText(resourceMap.getString("bNuevo.text")); // NOI18N
        bNuevo.setName("bNuevo"); // NOI18N
        bNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bNuevoActionPerformed(evt);
            }
        });

        bEditar.setIcon(resourceMap.getIcon("bEditar.icon")); // NOI18N
        bEditar.setText(resourceMap.getString("bEditar.text")); // NOI18N
        bEditar.setName("bEditar"); // NOI18N
        bEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEditarActionPerformed(evt);
            }
        });

        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setIcon(resourceMap.getIcon("jLabel2.icon")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(315, 315, 315)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 124, Short.MAX_VALUE)
                            .addComponent(bNuevo, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                            .addComponent(bEditar, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(66, 66, 66)
                        .addComponent(bNuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(100, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rNombreActionPerformed
        
        if(rNombre.isSelected()){
            nCedula.setSelected(false);
            nCarne.setSelected(false);
            consulta = "WHERE ((telDos.idTelefono % 2) = 0 AND (tel.idTelefono % 2) > 0 ) and  concat(Nombre_estudiante,' ', Apellido_estudiante) LIKE '%";
        }
    }//GEN-LAST:event_rNombreActionPerformed

    private void nCarneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nCarneActionPerformed
        if(nCarne.isSelected()){
            rNombre.setSelected(false);
            nCedula.setSelected(false);
            consulta = "WHERE  ((telDos.idTelefono % 2) = 0 AND (tel.idTelefono % 2) > 0 ) and  e.NoCarne LIKE '%";
        }
    }//GEN-LAST:event_nCarneActionPerformed

    private void nCedulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nCedulaActionPerformed
        
        if(nCedula.isSelected()){
            nCarne.setSelected(false);
            rNombre.setSelected(false);
            consulta = "WHERE ((telDos.idTelefono % 2) = 0 AND (tel.idTelefono % 2) > 0 ) and  concat(reg,orden) LIKE '%";
        }
        
    }//GEN-LAST:event_nCedulaActionPerformed

  
  
  
  
    
    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
       
            Vector<String> tableHeaders = new Vector<String>();
            Vector tableData = new Vector();
            tableHeaders.add("No. Carné");
            tableHeaders.add("No. Bufete");
            tableHeaders.add("Nombre");
            tableHeaders.add("No. Cedula");
            tableHeaders.add("Telefóno");
            tableHeaders.add("Celular");
            tableHeaders.add("Correo Electronico");
            
               try { 
                rs = verCliente.VerDatos(consulta+tBusqueda.getText()+"%'"); 
                int cont = 0;
                
                while (rs.next()){
                cont++;     
                Vector<Object> oneRow = new Vector<Object>();
                oneRow.add(rs.getString("NoCarne"));
                oneRow.add(rs.getString("NoBufete"));
                oneRow.add(rs.getString("Nombre_estudiante") + " "+ rs.getString("Apellido_estudiante"));
                oneRow.add(rs.getString("reg")+" "+ rs.getString("orden"));
                oneRow.add(rs.getString("tel.Telefono"));
                oneRow.add(rs.getString("telDos.Telefono"));
                oneRow.add(rs.getString("email_estudiante"));
                tableData.add(oneRow);
                
                }
                if(cont > 0 ) {
                    tEstudiante.setModel(new DefaultTableModel(tableData, tableHeaders));
                }
                else{
                   JOptionPane.showMessageDialog(null,"No existen Registros");
                }
                 

        } catch (SQLException ex) {
            Logger.getLogger(BusquedaEstudiante.class.getName()).log(Level.SEVERE, null, ex);
        }
              
            
        
        
    }//GEN-LAST:event_bBuscarActionPerformed

    private void bEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEditarActionPerformed
        java.sql.ResultSet res2 =null;
         // EditarCliente formEditar = new EditarCliente();
            Ingresar_alumno formEditar= new Ingresar_alumno(1);
            formEditar.setTitle("Edición Estudiante");
            formEditar.SetUsuario("Admin");

            if(tEstudiante.getSelectedRowCount() == 0){
                JOptionPane.showMessageDialog(null,"Debe seleccionar el estudiante a editar");
            }else{
         
        try {
            System.out.print(tEstudiante.getValueAt(tEstudiante.getSelectedRow(),0).toString());
            res2 = verCliente.VerDatosAlumno(tEstudiante.getValueAt(tEstudiante.getSelectedRow(),0).toString());
            formEditar.verResultsetObtenerDatosEstudiante(res2,tEstudiante.getValueAt(tEstudiante.getSelectedRow(),0).toString());
            formEditar.LLenarTel();
            formEditar.SetUsuario(Usuario);
            formEditar.setVisible(true);
        } catch (SQLException ex) {
            
        }
            }
    }//GEN-LAST:event_bEditarActionPerformed

    private void bNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bNuevoActionPerformed
            Ingresar_alumno formEditar= new Ingresar_alumno(0);
            formEditar.setTitle("Nuevo Estudiante");
            formEditar.setVisible(true);
            formEditar.SetUsuario(Usuario);
    }//GEN-LAST:event_bNuevoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
                
       Report Ejecutar = new Report();
     if(tEstudiante.getSelectedRowCount()== 0){
           Ejecutar.ReporteEstudiante("");
     }  
       else
        {
           Ejecutar.ReporteEstudiante(tEstudiante.getValueAt(tEstudiante.getSelectedRow(),0).toString().trim());
       }
       
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       Report Ejecutar = new Report();
     if(tEstudiante.getSelectedRowCount()== 0){
           Ejecutar.Caso("","");
     }  
       else
        {
           Ejecutar.Caso(tEstudiante.getValueAt(tEstudiante.getSelectedRow(),0).toString().trim(), "");
       }
       
    }//GEN-LAST:event_jButton2ActionPerformed

    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new BusquedaEstudiante().setVisible(true);
            }
        });
    }
    
    Connection conn=null;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bEditar;
    private javax.swing.JButton bNuevo;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton nCarne;
    private javax.swing.JRadioButton nCedula;
    private javax.swing.JRadioButton rNombre;
    private javax.swing.JTextField tBusqueda;
    private javax.swing.JTable tEstudiante;
    // End of variables declaration//GEN-END:variables
}
